<template>
  <el-form ref="chemistryRef" :model="chemistryform" label-width="110px" label-position="right" :rules="chemistryrules">
    <el-form-item label="经度：" prop="longitude">
      <el-input v-model="chemistryform.longitude"></el-input>
    </el-form-item>
    <el-form-item label="纬度：" prop="latitude">
      <el-input v-model="chemistryform.latitude"></el-input>
    </el-form-item>
    <el-form-item label="当量：" prop="currentDisplay">
      <el-input v-model="chemistryform.currentDisplay"></el-input>
    </el-form-item>
    <el-form-item label="危险物质：" prop="hazardousMaterial">
      <el-input v-model="chemistryform.hazardousMaterial"></el-input>
    </el-form-item>
    <el-form-item label="毒气扩散：" prop="toxicGasDiffusion">
      <el-input v-model="chemistryform.toxicGasDiffusion"></el-input>
    </el-form-item>
    <el-form-item label="风速：" prop="windSpeedMu">
      <el-input v-model="chemistryform.windSpeedMu"></el-input>
    </el-form-item>
    <el-form-item label="风向：" prop="windDirection">
      <el-input v-model="chemistryform.windDirection"></el-input>
    </el-form-item>
    <el-form-item label="燃烧速率：" prop="combustionRate">
      <el-input v-model="chemistryform.combustionRate"></el-input>
    </el-form-item>
    <el-form-item label="热值：" prop="calorificValueH">
      <el-input v-model="chemistryform.calorificValueH"></el-input>
    </el-form-item>
    <el-form-item label="热辐射效率：" prop="thermalRadiationEfficiency">
      <el-input v-model="chemistryform.thermalRadiationEfficiency"></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, onMounted, watchEffect } from 'vue'
import { validateNumber } from '@/utils/validate'


const props = defineProps({
 
  formtext: {
    type: String,
    default: ''
  }
})

const emit = defineEmits([
  'formsubmit',
  'cancel',
  'reset',
])

const chemistryform = ref({
  longitude: "",
  latitude: "",
  currentDisplay: "",
  hazardousMaterial: "",
  toxicGasDiffusion: "",
  windSpeedMu: "",
  windDirection: "",
  combustionRate: "",
  calorificValueH: "",
  thermalRadiationEfficiency: ""
})

const chemistryRef = ref()

const chemistryrules = ref({
  longitude: [{

    required: true, message: '请输入经度：', trigger: 'blur'
  }],
  latitude: [{
    required: true, message: '请输入纬度：', trigger: 'blur'

  }],
  currentDisplay: [{

    required: true, message: '请输入当量：', trigger: 'blur'
  },
  { validator: validateNumber, trigger: 'blur' }],
  hazardousMaterial: [{
    required: true, message: '请输入危险物质：', trigger: 'blur'

  }],
  toxicGasDiffusion: [{

    required: true, message: '请输入毒气扩散：', trigger: 'blur'
  }],
  windSpeedMu: [{

    required: true, message: '请输入风速：', trigger: 'blur'
  },
  { validator: validateNumber, trigger: 'blur' }],
  windDirection: [{
    required: true, message: '请输入风向：', trigger: 'blur'

  }],
  combustionRate: [{
    required: true, message: '请输入燃烧速率：', trigger: 'blur'

  },
  { validator: validateNumber, trigger: 'blur' }],
  calorificValueH: [{

    required: true, message: '请输入热值：', trigger: 'blur'
  },
  { validator: validateNumber, trigger: 'blur' }],
  thermalRadiationEfficiency: [{
    required: true, message: '请输入热辐射效率：', trigger: 'blur'

  },
  { validator: validateNumber, trigger: 'blur' }],
})


const handleSubmit = async (btntype) => {
  if (!chemistryRef.value) return
  try {
    await chemistryRef.value.validate((valid) => {
      if (valid) {

        chemistryform.value.currentDisplay = Number(chemistryform.value.currentDisplay)
        chemistryform.value.windSpeedMu = Number(chemistryform.value.windSpeedMu)
        chemistryform.value.combustionRate = Number(chemistryform.value.combustionRate)
        chemistryform.value.calorificValueH = Number(chemistryform.value.calorificValueH)
        chemistryform.value.thermalRadiationEfficiency = Number(chemistryform.value.thermalRadiationEfficiency)

        if (btntype == '模板') {
          emit('formsubmit', chemistryform.value, { btntype: btntype })
        } else {
          emit('formsubmit', chemistryform.value)
        }

      }
    })
  } catch {

  }
}

const handleReset = () => {
  if (chemistryRef.value) {
    chemistryRef.value.resetFields()
    // 重置表单数据为初始值
    Object.keys(chemistryform.value).forEach(key => {
      chemistryform.value[key] = ''
    })
  }
}

defineExpose({
  handleSubmit,
  handleReset,
})

onMounted(() => { })


watchEffect(() => {
  let data = sessionStorage.getItem('mnxl_data')
  if (data) {
    data = JSON.parse(data)
    if (data.simulatedformdata.simulateEventType == props.formtext) {
      chemistryform.value = data.formdatadata
    }
  }
})
</script>

<style lang="less" scoped></style>